<!--用户管理页-->
<template>
    <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
        <span style="line-height:60px;font-size:20px;">角色管理</span>
                <el-button @click="dialogVisible=true" type="primary" style="float:right;margin-top:13px;">新建角色</el-button>
    </div>
<!--    新增弹窗-->
    <el-dialog :title="dialogTitle" v-model="dialogVisible" :before-close="handleClose"
               style="width:1000px;padding:40px;">
        <el-form label-width="80px" label-position="top">
            <el-row :gutter="10">
                <el-col :span="12">
                    <el-form-item label="角色名">
                        <el-input placeholder="请输入角色名"
                                  v-model="saveRoleForm.roleName" ></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="角色类型">
                        <el-select placeholder="请选择" v-model="saveRoleForm.roleType">
                            <el-option label="超级管理员" value="0"></el-option>
                            <el-option label="普通管理员" value="1"></el-option>
                            <el-option label="普通员工" value="2"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>

            </el-row>
            <el-row :gutter="10">
                <el-col :span="12">
                    <el-form-item label="角色描述">
                        <el-input placeholder="请输入角色描述"
                                  v-model="saveRoleForm.roleDescription" ></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="角色状态">
                        <el-radio-group v-model="saveRoleForm.status">
                            <el-radio value="1" border style="margin:0;">启用</el-radio>
                            <el-radio value="0" border>禁用</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </el-col>
            </el-row>

        </el-form>
        <template #footer>
            <el-button @click="handleClose">取消</el-button>
            <el-button type="primary" @click="saveRole">保存</el-button>
        </template>
    </el-dialog>

    <!-- 用户查询卡片 -->
    <el-card style="margin:20px;height: 70px;">
        <el-form :inline="true">
            <el-form-item label="角色搜索">
                <el-input placeholder="输入角色名" style="width:220px;" v-model="searchRoleForm.roleName"
                          @keydown.enter="loadRole"></el-input>
            </el-form-item>

            <el-form-item label="角色状态" style="width:290px;">
                <el-select placeholder="请输入角色状态" v-model="searchRoleForm.status" @change="loadRole">
                    <el-option label="启用" value="1"/>
                    <el-option label="禁用" value="0"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button @click="resetSearch">重置</el-button>
                <el-button type="primary" @click="loadRole">查询</el-button>
            </el-form-item>
        </el-form>
    </el-card>
  <!-- 用户信息表格 -->
    <el-card style="margin:20px;">
        <el-table :data="roleArr">
            <el-table-column type="index" label="编号" width="80" align="center"/>
            <el-table-column prop="roleName" label="角色名" align="center"/>
            <el-table-column prop="roleType" label="角色类型" align="center"
                             :formatter="formatRoleType"></el-table-column>
            <el-table-column prop="createdAt" label="创建时间" align="center"/>
            <el-table-column prop="status" label="用户状态" align="center">
                <template #default="scope">
                    <!--active-value="1" inactive-value="0"设置开关激活的值为1,关闭的值为0开关提交的值与status双向绑定-->
                    <el-switch @change="changeStatus(scope.row.id,scope.row.status)" active-value="1" inactive-value="0"
                               v-model="scope.row.status"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template #default="scope">
                    <!-- :disabled="scope.row.level==40" 不能对总裁进行更改状态、密码重置、编辑、删除操作 -->
                    <el-button link type="primary" size="small" @click="editRole(scope.row.id)">编辑</el-button>
                    <el-button link type="primary" size="small" @click="deleteRole(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";

const dialogTitle=ref('新增角色');
const dialogVisible = ref(false);
const saveRoleForm = ref({
    roleName: '',
    roleType: '',
    status: '',
    roleDescription:'',
    createdAt: ''
});
//新增员工
const saveRole = () => {
    let data = qs.stringify(saveRoleForm.value);
    console.log(data);
    axios.post(BASE_URL+'/v1/role/save',data).then((response) => {
        if (response.data.code == 2000) {
            ElMessage.success('保存成功!');
            dialogVisible.value = false;//关闭弹窗
            saveRoleForm.value={};//清空输入的数据,防止新增成功后打开弹窗还是之前的数据
            loadRole();
        } else {
            ElMessage.error(response.data.msg);
        }
    })
}

//实现取消按钮的功能
const handleClose = ()=>{
    if(confirm('是否关闭本窗口?')){
        //用户确认关闭后,将审批弹窗与确认驳回弹窗一起关掉
        dialogVisible.value = false;
        //取消时把填的数据都清空，表单回复到初始状态
        saveRoleForm.value={};
    }
}

//定义数组用来装角色列表信息
const roleArr = ref([]);
//定义对象保存查询条件
const searchRoleForm = ref({roleName: '', status: ''});
const loadRole = () => {
    //如果传入了查询条件，则根据条件查询，没有条件，默认查询所有员工信息
    let data = qs.stringify(searchRoleForm.value);
    console.log(data)
    axios.get(BASE_URL + '/v1/role/select?' + data).then((response) => {
        if (response.data.code == 2000) {
            roleArr.value = response.data.data;
            console.log(roleArr.value);
        } else {
            ElMessage.error(response.data.msg);
        }
    })
}

//页面一加载立即调用
onMounted(() => {
    loadRole();//加载员工列表
})

//重置用户搜索信息
const resetSearch = () => {
    searchRoleForm.value = {};
    loadRole();
}
/*
//密码重置
const resetPassword = (userId)=>{
    axios.post(BASE_URL+'/v1/user/update/password/'+userId).then((response) => {
        if (response.data.code === 2000) {
            ElMessage.success('重置成功!');
        } else {
            ElMessage.error(response.data.msg);
        }
    })
}
*/
//编辑员工
const editRole = (id) => {
    //修改弹窗标题并打开弹窗
    dialogTitle.value = '编辑角色';
    dialogVisible.value = true;
    //根据id查询要修改的当前员工的信息
    axios.get(BASE_URL + '/v1/role/select?id=' + id).then((response) => {
        if (response.data.code == 2000) {
            saveRoleForm.value = response.data.data[0];
        } else {
            ElMessage.error(response.data.msg);
        }
    })
}
//类型转换
const formatRoleType = (row, column, cellValue, index) => {
    if (cellValue == 0) {
        cellValue = '超级管理员';
    }else if (cellValue == 1){
        cellValue='普通管理员';
    }else if (cellValue == 2){
        cellValue='普通用户';
    }
    return cellValue;
}
//修改用户状态
const changeStatus = (id,status) => {
    axios.post(BASE_URL+'/v1/role/update/status/'+id+'/'+status).then((response) => {
        if (response.data.code == 2000) {
            ElMessage.success('修改成功!');
        } else {
            ElMessage.error(response.data.msg);
            //数据库没修改成功的话，就需要把当前的用户状态重置为修改前的状态
            saveRoleForm.status = !saveRoleForm.status;
        }
    })
}

//删除用户
const deleteRole = (id) => {
    if (confirm('您确认要删除此角色吗?')) {
        axios.post(BASE_URL + '/v1/role/delete/' + id).then((response) => {
            if (response.data.code == 2000) {
                ElMessage.success('删除成功!');
                loadRole();
            } else {
                ElMessage.error(response.data.msg);
            }
        })
    }
}
</script>

<style scoped>

</style>